﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using NetTao.Model;

namespace NetTao.Dao
{
    public class DaoSysMatchs : DaoBase
    {

        public DaoSysMatchs(string connectionString)
            : base(connectionString)
        {
            ConnectionString = connectionString;
        }

        /// <summary>
        /// 是否存在该记录,true为存在,false为不存在
        /// </summary>
        public bool Exists(int articleid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from sysmatchs");
            strSql.Append(" where articleid=?articleid ");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32,11)			};
            parameters[0].Value = articleid;
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters))
            {
                while (dr.Read())
                {
                    return dr[0].ToString() != "0";
                }
            }
            return false;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SysMatchsInfo model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into sysmatchs(");
            strSql.Append("webflag,articleid,automatch,matchdate,matchtime)");
            strSql.Append(" values (");
            strSql.Append("?webflag,?articleid,?automatch,?matchdate,?matchtime)");
            MySqlParameter[] parameters =
                {
                    new MySqlParameter("?webflag", MySqlDbType.String, 10),
                    new MySqlParameter("?articleid", MySqlDbType.Int32, 11),
                    new MySqlParameter("?automatch", MySqlDbType.Int32, 11),
                    new MySqlParameter("?matchdate", MySqlDbType.Int32, 11),
                    new MySqlParameter("?matchtime", MySqlDbType.Int32, 11)
                };
            parameters[0].Value = model.webflag;
            parameters[1].Value = model.articleid;
            parameters[2].Value = model.automatch;
            parameters[3].Value = model.matchdate;
            parameters[4].Value = model.matchtime;

            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text,
                                               strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SysMatchsInfo model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update sysmatchs set ");
            strSql.Append("webflag=?webflag,");
            strSql.Append("articleid=?articleid,");
            strSql.Append("automatch=?automatch,");
            strSql.Append("matchdate=?matchdate,");
            strSql.Append("matchtime=?matchtime");
            strSql.Append(" where id=?id");
            MySqlParameter[] parameters = {
					new MySqlParameter("?webflag", MySqlDbType.String,11),
					new MySqlParameter("?articleid", MySqlDbType.Int32,11),
					new MySqlParameter("?automatch", MySqlDbType.Int32,11),
					new MySqlParameter("?matchdate", MySqlDbType.Int32,11),
					new MySqlParameter("?matchtime", MySqlDbType.Int32,11),
					new MySqlParameter("?id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.webflag;
            parameters[1].Value = model.articleid;
            parameters[2].Value = model.automatch;
            parameters[3].Value = model.matchdate;
            parameters[4].Value = model.matchtime;
            parameters[5].Value = model.id;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text,
                                                 strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NetTao.Model.SysMatchsInfo GetModel(int articleid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select id,webflag,articleid,automatch,matchdate,matchtime from sysmatchs ");
            strSql.Append(" where articleid=?articleid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32)
			};
            parameters[0].Value = articleid;
            var model = new SysMatchsInfo();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters))
            {
                while (dr.Read())
                {
                    if (dr["id"] != null && dr["id"].ToString() != "")
                    {
                        model.id = int.Parse(dr["id"].ToString());
                    }
                    if (dr["webflag"] != null && dr["webflag"].ToString() != "")
                    {
                        model.webflag = dr["webflag"].ToString();
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["automatch"] != null && dr["automatch"].ToString() != "")
                    {
                        model.automatch = int.Parse(dr["automatch"].ToString());
                    }
                    if (dr["matchdate"] != null && dr["matchdate"].ToString() != "")
                    {
                        model.matchdate = int.Parse(dr["matchdate"].ToString());
                    }
                    if (dr["matchtime"] != null && dr["matchtime"].ToString() != "")
                    {
                        model.matchtime = int.Parse(dr["matchtime"].ToString());
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int articleid,string webflag)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from sysmatchs ");
            strSql.Append(" where articleid=?articleid and webflag=?webflag");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32),
                    new MySqlParameter("?webflag", MySqlDbType.String)
			};
            parameters[0].Value = articleid;
            parameters[1].Value = webflag;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text,
                                                 strSql.ToString(), parameters) >= 0;
        }
    }
}
